import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:get/get.dart';
import 'package:home_box/pages/medicine/MedicineM/medicine_model.dart';
import 'package:image_picker/image_picker.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

import '../MedicineM/medicine_service.dart';

class MedicineVM extends GetxController {
  var takePic = ''.obs;
  var medicine = Medicine().obs;
  var dailyFrequencyIndex = 0.obs;
  var dosageIndex = 0.obs;
  var dosageUnitIndex = 0.obs;
  var targetIndex = 0.obs;
  TextEditingController nameController = TextEditingController();
  TextEditingController userNameController = TextEditingController();
  TextEditingController dosageController = TextEditingController();
  TextEditingController indicationController = TextEditingController();
  final IsarService _isarService = IsarService();
  late Future<Isar> _isarInstance;

  @override
  onInit() {
    super.onInit();
    _isarInstance = _isarService.isarInstance;
  }

  @override
  onClose() {
    super.onClose();
    _isarService.closeIsar();
  }

  ///从相机获取图片
  takePicFromCamera() async {
    ImagePicker picker = ImagePicker();
    XFile? photo = await picker.pickImage(source: ImageSource.camera);
    takePic.value = photo!.path;
  }

  ///图片压缩并Base64
  Future<String?> _pictureCompressAndBase64(String filePath) async {
    var result = FlutterImageCompress.compressWithFile(
      filePath,
      minWidth: 1080,
      minHeight: 720,
      quality: 60,
    );
    return base64Encode(result as List<int>);
  }

  ///更新名称
  void updateName(String name) {
    medicine.update((m) => m?.name = name);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新生产日期
  void updateProductDate(DateTime d) {
    medicine.update((m) => m?.productionDate = d);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新有效日期
  void updateExpiryData(DateTime d) {
    medicine.update((m) => m?.expiryData = d);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新用法
  void updateDailyFrequency(String times) {
    medicine.update((m) => m?.dailyFrequency = times);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新用量
  void updateDosage(int count) {
    medicine.update((m) => m?.dosage = count);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新用量单位
  void updateDosageUnit(DosageUnits unit) {
    medicine.update((m) => m?.unit = unit);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新使用者姓名
  void updateUserName(String userName) {
    medicine.update((m) => m?.user = userName);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新使用人群
  void updateTarget(Target t) {
    medicine.update((m) => m?.target = t);

    ///如果使用GetBuilder,还需要手动调用update方法，否则UI不会自动刷新
    update();
  }

  ///更新适用证
  void updateIndication(String indication) {
    medicine.update((m) => m?.indication = indication);
    update();
  }

  ///添加药品
  Future<int> addMedicine() async {
    final isar = await _isarInstance;
    return await isar.writeTxn(() async {
      return await isar.medicines.put(medicine.value);
    });
  }

  ///更新药品
  Future<void> updateMedicine(Medicine medicine) async {
    final isar = await _isarInstance;
    await isar.writeTxn(() async {
      await isar.medicines.put(medicine);
    });
  }

  ///删除数据
  Future<void> deleteMedicine(int id) async {
    final isar = await _isarInstance;
    await isar.writeTxn(() async {
      await isar.medicines.delete(id);
    });
  }

  ///查询
  Future<List<Medicine>> getAllMedicine() async {
    final isar = await _isarInstance;
    return await isar.medicines.where().findAll();
  }
}
